package com.jyh.blog.controller;

import com.jyh.blog.annotation.OptLog;
import com.jyh.blog.dto.TagBackDTO;
import com.jyh.blog.dto.TagDTO;
import com.jyh.blog.entity.Tag;
import com.jyh.blog.service.TagService;
import com.jyh.blog.utils.Result;
import com.jyh.blog.vo.ConditionVO;
import com.jyh.blog.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jyh.blog.constant.OptTypeConst.*;

@Api(tags = "标签管理")
@RestController
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 前端查询所有
     * @return
     */
    @ApiOperation(value = "查询首页标签")
    @GetMapping("/tags")
    public Result<List<TagDTO>> list(){
        return Result.ok(tagService.tagList());
    }

    /**
     *条件查询所有（后台）
     * @return
     */
    @ApiOperation(value = "查询后台标签")
    @PostMapping("/admin/tags")
    public Result list(@RequestBody ConditionVO condition){
        PageResult<TagBackDTO> list = tagService.tagListBack(condition);
        return Result.ok(list);
    }

    /**
     * 添加或修改标签
     * @return
     */
    @OptLog(optType = SAVE_OR_UPDATE)
    @ApiOperation(value = "添加或修改标签")
    @PostMapping("/admin/tag")
    public Result addOrUpdate(@RequestBody Tag tag){
        tagService.addOrUpdate(tag);
        return Result.ok();
    }

    /**
     * 删除标签
     * @return
     */
    @OptLog(optType = REMOVE)
    @ApiOperation(value = "通过id删除标签")
    @DeleteMapping("/admin/tag/{id}")
    public Result deleteById(@PathVariable String id){
        tagService.deleteTagById(id);
        return Result.ok();
    }
}
